package com.sjl.impl.user;

import cn.hutool.log.StaticLog;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjl.filter.Demo;
import com.sjl.mapper.UserMapper;
import com.sjl.mapper.comm.GenericDao;
import com.sjl.springboot.domain.CommonResponse;
import com.sjl.springboot.domain.enums.CommonCode;
import com.sjl.springboot.domain.page.PageQuery;
import com.sjl.springboot.domain.user.SysUser;
import com.sjl.springboot.inface.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户业务实现类
 *
 * @author sjl
 * @date 2018年6月1日 16:33:35
 */
@Service
@org.springframework.stereotype.Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private GenericDao genericDao;
    /**
     * sys_user
     * 表主键
     */
    private final static String KEY_NAME = "userId";

    @Override
    public List<SysUser> findAllUsers() {
        List<SysUser> userList = userMapper.findAllUsers();
        if (userList != null) {
            return userList;
        } else {
            return null;
        }
    }

    @Override
    public CommonResponse<SysUser> findUserById(String id) {
        CommonResponse<SysUser> response = new CommonResponse<>(CommonCode.Select_false);
        if (StringUtils.isBlank(id)) {
            response.setContent(CommonCode.Paramter_error);
            return response;
        }
        SysUser sysUser = userMapper.findUserById(id);
        if (sysUser != null) {
            response.setContent(CommonCode.Success_msg);
            response.setResult(sysUser);
        }

        return response;
    }

    @Override
    public CommonResponse<SysUser> findPageUsers(PageQuery<SysUser> pageQuery) {
        CommonResponse<SysUser> response = new CommonResponse<>(CommonCode.Success_msg);
        //使用分页插件,核心代码就这一行
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getSize());
        PageInfo<SysUser> pageInfo = new PageInfo<>(userMapper.findPageUsers(pageQuery));
        response.setPageInfo(pageInfo);

        return response;
    }

    @Override
    @Demo
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class, isolation = Isolation.DEFAULT)
    public CommonResponse updateUser(SysUser sysUser) {
        int result;
        CommonResponse<Integer> response = new CommonResponse<>(CommonCode.Update_false);
        result = genericDao.updateNonNullById(sysUser, KEY_NAME);
        if (result > 0) {
            response.setContent(CommonCode.Success_msg);
            response.setResult(result);
        }
        return response;
    }

    @Override
    public CommonResponse<SysUser> getUserByName(String name) {
        CommonResponse<SysUser> response = new CommonResponse<>(CommonCode.Select_false);
        if (StringUtils.isNotBlank(name)) {
            SysUser sysUser = userMapper.finUserByName(name);
            if(sysUser!=null){
                response.setResult(sysUser);
                response.setContent(CommonCode.Success_msg);
            }else{
                response.setContent(CommonCode.Result_empty);
            }

        }else{
            response.setContent(CommonCode.Paramter_error);
        }
        return response;
    }
}
